﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Xml;
namespace HeavenvsDarkness
{ 
    public class MainMenuScreen : MyForm
    {
        public const int NEW_GAME = 0;
        public const int RESUME = 1;
        public const int OPTION = 2;
        public const int HELP = 3;
        public const int EXIT = 4;
        public const int MaxMenuItem = 5;


        Vector3 deltaMoveMenu;

        public Vector3 DeltaMoveMenu
        {
            get { return deltaMoveMenu; }
            set { deltaMoveMenu = value; }
        }

        float rotationOfMenu;

        public float RotationOfMenu
        {
            get { return rotationOfMenu; }
            set { rotationOfMenu = value; }
        }
        List<int> listSorted;

        public List<int> ListSorted
        {
            get { return listSorted; }
            set { listSorted = value; }
        }

        List<MyMenuItem> menuItems;

        public List<MyMenuItem> MenuItems
        {
            get { return menuItems; }
            set { menuItems = value; }
        }
        int indexChosen;

        public int IndexChosen
        {
            get { return indexChosen; }
            set 
            {
                if (IndexChosen != value || value == 0)
                {               
                    indexChosen = value;
                    MenuItems[IndexChosen].SetChosenScale();
                    ListSorted.Clear();
                    ListSorted.Add(IndexChosen);
                    int indexItem = indexChosen;
                    Boolean isFullAside = false;
                    while (ListSorted.Count != MaxMenuItem)
                    {
                        if (ListSorted.Count == MaxMenuItem / 2 + 1)
                        {                           
                            isFullAside = true;
                            indexItem = IndexChosen;
                        }


                        if (isFullAside)
                        {
                            indexItem = (indexItem + MenuItems.Count - 1) % MenuItems.Count;
                            ListSorted.Insert(0, indexItem);
                        }
                        else
                        {
                            indexItem = (indexItem + 1) % MenuItems.Count;
                            ListSorted.Add(indexItem);
                        }
                        MenuItems[indexItem].SetDefaultScale();
                    }
          
                    for (int i = 0; i < ListSorted.Count; i++)
                    {
                        MenuItems[ListSorted[i]].DestPost = (ListSorted.Count / 2 - i) * DeltaMoveMenu;                      
                        MenuItems[ListSorted[i]].Rotation = Matrix.CreateRotationX(-(ListSorted.Count / 2 - i) * RotationOfMenu);
                    }
                }
                else
                {
                    indexChosen = value;
                }
                            
            }
        }

        float speedCloseMenu;

        public float SpeedCloseMenu
        {
            get { return speedCloseMenu; }
            set { speedCloseMenu = value; }
        }
       

        public MainMenuScreen()
        {
            ListSorted = new List<int>();
            Items = new List<VisibleGameEntity>();
            MenuItems = new List<MyMenuItem>();
            InitComponent();
            
        }

        public void InitComponent()
        {
            XmlDocument Doc = new XmlDocument();
            Doc.Load("ConfigureInterface.xml");
           
            XmlNode nodeMainMenu = Doc.SelectSingleNode("/Game/Form[@Name='MainMenu']");

            XmlNodeList Child=nodeMainMenu.ChildNodes;
            for (int i = 0; i < Child.Count; i++)
            {
                XmlElement ele = (XmlElement)Child[i];
                if (ele.Name == "Sprites")
                {
                    XmlNodeList SpriteList = ele.ChildNodes;
                    for (int j = 0; j < SpriteList.Count; j++)
                    {

                        XmlElement eleSprite = (XmlElement)SpriteList[j];
                        MySprite temp = new MySprite(eleSprite);
                        Items.Add(temp);
                    }
                }
                if (ele.Name == "Menu")
                {
                    Vector3 deltaMove;
                    deltaMove.X = float.Parse(ele.GetAttribute("DeltaX"));
                    deltaMove.Y = float.Parse(ele.GetAttribute("DeltaY"));
                    deltaMove.Z = float.Parse(ele.GetAttribute("DeltaZ"));
                    RotationOfMenu = float.Parse(ele.GetAttribute("RotationX"));
                    SpeedCloseMenu = float.Parse(ele.GetAttribute("SpeedCloseMenu")); 
                    DeltaMoveMenu = deltaMove;

                    XmlNodeList menuList = ele.ChildNodes;
                    foreach (XmlNode node in menuList)
                    {
                        XmlElement eleMenu = (XmlElement)node;
                        MyMenuItem temp = new MyMenuItem(eleMenu);
                        MenuItems.Add(temp);
                        Global.Items.Add(temp);
                    }
                    IndexChosen = int.Parse(ele.GetAttribute("IndexDefault"));
                   
                }
            }

        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            if (NextForm != null)
            {
                NextForm.Draw(spriteBatch);
            }

            for(int i=0;i<Items.Count;i++)
            {
                Items[i].Draw(spriteBatch);
            }

            for (int i = 0; i < ListSorted.Count; i++)
            {
                MenuItems[ListSorted[i]].Draw(spriteBatch);
            }

            
        }

        public override void Update(GameTime gameTime)
        {
            if (NextForm != null && Items[0].Position == Items[0].DestPost)
            {
                IsCanbeSwitch = true;
            }

            for (int i = 0; i < Items.Count; i++)
            {
                Items[i].Update(gameTime);
            }

            for (int i = 0; i < MenuItems.Count; i++)
            {
                MenuItems[i].Update(gameTime);
            }
        }

        public override int GetIndex()
        {
            return IndexChosen;
        }
      /*  public override void DrawEffectCloseForm()
        {
            IsStop = true;
            for (int i = 0; i < Items.Count; i++)
            {
                if (Items[i].GetType() == (new MyMenu()).GetType())
                {
                    MyMenu temp = (MyMenu)Items[i];
                    temp.IsStop = true;
                }
            }
           
        }

        public override MyForm SwitchScreen()
        {
            MyForm temp = null;
            if (GetIndex() != -1)
            {
                if (CanBeSwitch())
                {
                    switch (IndexChosen)
                    {
                        case NEW_GAME:
                            temp = new ChooseForceScreen();
                            break;
                    }
                }
                else
                {
                    DrawEffectCloseForm();
                }
            }

            return temp;

        }

        public override bool CanBeSwitch()
        {
            for (int i = 0; i < Items.Count; i++)
            {
                if (Items[i].GetType() == (new MyMenu()).GetType())
                {
                    MyMenu temp = (MyMenu)Items[i];
                    return temp.CanBeSwitch();
                }
            }
            return false;
        }
        */
        public override bool OnEvent(Global.EVENT events, object sender, object arg)
        {
            switch (events)
            {
                case Global.EVENT.GM_MOUSE_RIGHTCLICKED:
                    int index=-1;
                        Console.WriteLine(MenuItems[IndexChosen].GetTextureSize()* MenuItems[IndexChosen].Scale);
                        Console.WriteLine(Global.GetCursor().GetPickedPosition());            
                    break;
                case Global.EVENT.GM_UPARROW_PRESSED:
                    if (IsClosed)
                    {
                        break;
                    }
                    IndexChosen = (IndexChosen + 1) % MenuItems.Count;
                    MenuItems[ListSorted[listSorted.Count-1]].Position = -(ListSorted.Count/2 + 1)*DeltaMoveMenu;                 
                    break;

                case Global.EVENT.GM_DOWNARROW_PRESSED:
                    if (IsClosed)
                    {
                        break;
                    }
                    IndexChosen = (IndexChosen + MenuItems.Count - 1) % MenuItems.Count;
                    MenuItems[ListSorted[0]].Position = (ListSorted.Count / 2 + 1) * DeltaMoveMenu;
                    break;

                case Global.EVENT.GM_ENTER_CLICKED:
                    IsClosed = true;
                    SwitchScreen();
                    break;

            }
            return false;
        }

        public override void SwitchScreen()
        {
            switch (IndexChosen)
            {
                case NEW_GAME:
                    NextForm = new ChoseForceScreen();
                    break;
            }

            Vector3 DestToClose = new Vector3(0, 0, 15f);
            MenuItems[IndexChosen].DestPost += DestToClose;
            MenuItems[IndexChosen].SpeedMove = Items[0].SpeedMove;
            for (int i = 0; i < Items.Count; i++)
            {
                Items[i].DestPost += DestToClose;
            }

            DestToClose = new Vector3(0, 5, 0);
            for (int i = 0; i < ListSorted.Count; i++)
            {
                if (ListSorted[i] != IndexChosen)
                {
                    MenuItems[ListSorted[i]].DestPost = (ListSorted.Count / 2 - i) * DestToClose;
                    MenuItems[ListSorted[i]].SpeedMove = SpeedCloseMenu;
                }
            }
        }
    }
}
